<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>随机生成指定长度的字符串</title>
</head>
<body>
    <script>
        //getLetter函数，将Unicode码点转换为字符串
        function getLetter(unicodeValue) {
            if(unicodeValue >= 0 && unicodeValue <= 0xFFFF){//0xFFFF是十六进制的FFFF，等于十进制的65535
                                                        //Unicode BMP范围
                return String.fromCharCode(unicodeValue);
        } else{
            throw new Error('Invalid Unicode value. It should be between 0 and 0xFFFF.');
        }
    }
    //randomString函数，生成一个长度为n的随机字符串
    function randomString(n){
        if(n <= 0 || !Number.isInteger(n)) {
            throw new Error('The length should be a positive integer.');
        }

        let result = '';
        for (let i = 0; i < n;i++) {
            //生成一个随机的Unicode码点
            const randomUnicode = Math.floor(Math.random() * (0xFFFF + 1));
            //调用getLetter函数将Unicode值转换为字符
            result += getLetter(randomUnicode);
        }
        return result;
    }
    //调用randomString函数，生成长度为3的随机字符串
    try {
        const randomStringResult = randomString(3);
        console.log(randomStringResult); //输出结果到控制台
    }catch (error) {
        console.error(error.message);
    }
    </script>
</body>
</html>